﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Common.Vod;
using AiXiu.Model;
using AiXiu.DAL;

namespace AiXiu.BLL
{
    public class VideoBLL
    {
        //实力化类对象
        VodHelper vodHelper = new VodHelper();
        VideoDAL videoDAL = new VideoDAL();
        /// <summary>
        /// 获取视频上传地址和凭证
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="fileName">文件名</param>
        /// <param name="location">位置</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public CreateUploadVideoResult UploadVideo(string title, string fileName, string location, int userId)
        {
            //调用VodHelper里面的方法获取视频上传地址和凭证
            CreateUploadVideoResult result = vodHelper.CreateUploadVideo(title, fileName);

            //数据库新增视频信息
            TBVideos video = new TBVideos();
            video.VideoId = result.VideoId;
            video.UserId = userId;
            video.Headline = title;
            video.Location = location;
            video.Status = (int)VideoStatus.Uploading;
            videoDAL.AddVideo(video);
            return result;
        }

        /// <summary>
        /// 同步视频信息
        /// </summary>
        /// <returns></returns>
        public int SyncVideos()
        {
            //调用DAL层方法——获取处理中的视频Id列表
            List<string> videoIds = videoDAL.GetVideoId();
            if (videoIds.Count > 0)
            {
                //调用VodHelper中【批量获取视频信息】的方法，获取视频信息结果类GetVideoInfosResult
                GetVideoInfosResult result = vodHelper.GetVideoInfos(videoIds);
                List<VideoInfo> VideoList = result.VideoList;
                List<TBVideos> tBVideos = new List<TBVideos>();
                foreach (VideoInfo videoInfo in VideoList)
                {
                    TBVideos video = new TBVideos();
                    video.VideoId = videoInfo.VideoId;
                    video.CoverURL = videoInfo.CoverURL;
                    video.Status = (int)videoInfo.Status;
                    tBVideos.Add(video);
                }

                //调用DAL层方法——批量更新视频信息
                videoDAL.UpdateVideos(tBVideos);
                return tBVideos.Count;

            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 视频列表显示
        /// </summary>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            return videoDAL.GetVideos();
        }

        /// <summary>
        /// 通过视频Id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            return videoDAL.GetVideoByVideoId(videoId);
        }
        /// <summary>
        /// 获取视频播放凭证
        /// </summary>
        /// <param name="videoId">视频Id</param>
        /// <returns></returns>
        public GetPlayInfoResult InitPlay(string videoId)
        {
            return vodHelper.GetPlayInfo(videoId);
        }

        /// <summary>
        /// 通过用户Id获取视频列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<TBVideos> GetVideosByUserId(int userId)
        {
            return videoDAL.GetVideosByUserId(userId);
        }
        /// <summary>
        /// 删除视频
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public OperResult Delete(string videoId)
        {
            //阿里云删除，调用VodHelper中 删除完整视频的方法
            vodHelper.DeleteVideos(new List<string> { videoId });
            //从数据库删除数据
            int res = videoDAL.DeleteVideo(videoId);
            //返回操作结果类OperResult
            if (res > 0)
            {
                return OperResult.Succeed();
            }
            else
            {
                return OperResult.Failed("数据库删除视频失败！");
            }
        }
    }
}
